﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.Model;
using Gift.WebApp;
using System.Data;

namespace WebApp.admin.warehouse
{
    public partial class warehouseDistribute : BaseAdmin
    {

        tbWarehouse oWarehouse;
        tbGoods oGoods;
     
        tbWarehouseGoods oWarehouseGoods;
      
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.WarehousesDistribute);
                this.BindWarehouse();
                this.BuildForm();
            }
        }

        void BuildForm()
        {
            oWarehouse = BaseFactory.theWarehouse.GetModel(int.Parse(Request.QueryString["wid"]));
            if (oWarehouse != null)
            {
                this.lblWarehouseName.Text = oWarehouse.warehouseName;
            }
            oGoods = BaseFactory.theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
            if(oGoods != null)
            {
                this.lblGoodsName.Text = oGoods.goodsName;
                this.lblGoodsNo.Text = oGoods.goodsNo;
            }
            this.lblBatchNo.Text = Request.QueryString["bno"];
            this.lblGoodsCount.Text = Request.QueryString["gcount"];

        }

        void BindWarehouse()
        {
            DataTable dt = BaseFactory.theWarehouse.GetList(" companyId = " + base.Administrator.CompanyId + " and id != " + Request.QueryString["wid"] ).Tables[0];
            if (dt != null)
            {
                this.ddlInWarehouse.DataTextField = "warehouseName";
                this.ddlInWarehouse.DataValueField = "id";
                this.ddlInWarehouse.DataSource = dt;
                this.ddlInWarehouse.DataBind();
            }
            this.ddlInWarehouse.Items.Insert(0, new ListItem("请选择仓库", ""));

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (int.Parse(this.lblGoodsCount.Text) < int.Parse(this.txtGoodsCount.Value))
            {
                ExtAspNet.Alert.Show("超出最大调拨数量限制！");
                return;
            }
         

            oWarehouseGoods = new tbWarehouseGoods();
            oWarehouseGoods.companyId = int.Parse(base.Administrator.CompanyId);
            oWarehouseGoods.creater = int.Parse(base.Administrator.AdminUserId);
            oWarehouseGoods.createTime = DateTime.Now;
            oWarehouseGoods.GoodsBatchNo = this.lblGoodsNo.Text;
            oWarehouseGoods.goodsCount = -int.Parse(this.txtGoodsCount.Value);
            oWarehouseGoods.goodsId = int.Parse(Request.QueryString["gid"]);
            oWarehouseGoods.remark = "调拨出库";
            oWarehouseGoods.warehouseId = int.Parse(Request.QueryString["wid"]);
            BaseFactory.theWarehouseGoods.Add(oWarehouseGoods);


            oWarehouseGoods = new tbWarehouseGoods();
            oWarehouseGoods.companyId = int.Parse(base.Administrator.CompanyId);
            oWarehouseGoods.creater = int.Parse(base.Administrator.AdminUserId);
            oWarehouseGoods.createTime = DateTime.Now;
            oWarehouseGoods.GoodsBatchNo = this.lblGoodsNo.Text;
            oWarehouseGoods.goodsCount = int.Parse(this.txtGoodsCount.Value);
            oWarehouseGoods.goodsId = int.Parse(Request.QueryString["gid"]);
            oWarehouseGoods.remark = this.txtaRemark.Value.Trim();
            oWarehouseGoods.warehouseId = int.Parse(this.ddlInWarehouse.SelectedItem.Value);
            BaseFactory.theWarehouseGoods.Add(oWarehouseGoods);

            ExtAspNet.Alert.Show("调拨成功！", "", "window.location.href='warehouseDistributeList.aspx';");
        }
    }
}